<%@page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%-- <%@ page trimDirectiveWhitespaces="true" %> --%>
<%@ include file="/WEB-INF/jsp/generic/taglibs.jsp"%>
<s:layout-render name="/WEB-INF/jsp/generic/layout.jsp">
	
	<s:layout-component name="css">
	<link rel="stylesheet" type="text/css" href="${contextPath}/css/admin.css">
	</s:layout-component>

	<s:layout-component name="pageContent">
	<div class="content">
	
		<c:set var="edit">${not empty actionBean.newMember and not empty actionBean.newMember.id}</c:set>
		<c:set var="collapsed">${edit ? "" : "collapsed" }</c:set>


		<s:messages key="${actionBean.newMemberKey }"/>
		<s:messages key="${actionBean.deleteMemberKey }"/>
		<%-- <s:messages key="${actionBean.deleteRoleKey }"/>
		<s:messages key="${actionBean.newRoleKey }"/> --%>
		
		<%-- <s:form beanclass="afcp.alumni.action.page.AdminAction"><s:submit name="sendmail">Envoyer un email</s:submit></s:form> --%>
		
		<h2>Gestion des membres</h2>
		
		<fieldset>
			<legend>Cliquez ici pour ajouter un membre</legend>
			<s:form beanclass="afcp.alumni.action.page.AdminAction" class="${collapsed }"><%-- class="collapsed" --%><%-- action="${actionBean.manageMemberUrl }" --%><!--  beanclass="afcp.alumni.action.event.ManageMemberAction" -->
				<s:errors globalErrorsOnly="true" /><!-- afcp.alumni.action.page.AdminAction -->
				<table>
					<tr>
						<td><s:label for="newMember.email" /></td>
						<td>
							
							<s:text name="newMember.email" id="newMember.email"
									disabled="${edit}" />
							<s:hidden name="newMember.email" />

								<%-- <c:choose>
							<c:when test="${not empty actionBean.newMember and not empty actionBean.newMember.id }">
								<s:text name="newMember.email" id="newMember.email" disabled="disabled" />
							</c:when>
							<c:otherwise>
								<s:text name="newMember.email" id="newMember.email" />
							</c:otherwise>
							</c:choose> --%>
						
						 	<s:errors field="newMember.email" />
						</td>
					</tr>
					<tr>
						<td><s:label for="newMember.lastName" /></td>
						<td><s:text name="newMember.lastName" id="newMember.lastName" /> <s:errors
								field="newMember.lastName" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.firstName" /></td>
						<td><s:text name="newMember.firstName" id="newMember.firstName" /> <s:errors
								field="newMember.firstName" /></td>
					</tr>
					
					<tr>
						<td><s:label for="newMember.sex" /></td>
						<td><s:select name="newMember.sex" id="newMember.sex">
								<s:options-enumeration enum="afcp.alumni.model.db.Member.Sex"/>
							</s:select> <s:errors field="newMember.sex" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.chineseName" /></td>
						<td><s:text name="newMember.chineseName" id="newMember.chineseName" /> <s:errors
								field="newMember.chineseName" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.birthDate" /></td>
						<td><s:text formatType="date" formatPattern="dd/MM/yyyy"
								name="newMember.birthDate" id="newMember.birthDate" /> <s:errors
								field="newMember.birthDate" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.parisTechEntranceYear" /></td>
						<td><s:text name="newMember.parisTechEntranceYear" id="newMember.parisTechEntranceYear" /> <s:errors
								field="newMember.parisTechEntranceYear" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.parisTechSchool" /></td>
						<td><s:text name="newMember.parisTechSchool" id="newMember.parisTechSchool" /> <s:errors
								field="newMember.parisTechSchool" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.chineseUniversity" /></td>
						<td><s:text name="newMember.chineseUniversity" id="newMember.chineseUniversity" /> <s:errors
								field="newMember.chineseUniversity" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.scholarship" /></td>
						<td><s:text name="newMember.scholarship" id="newMember.scholarship" /> <s:errors
								field="newMember.scholarship" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.domainInFrance" /></td>
						<td><s:text name="newMember.domainInFrance" id="newMember.domainInFrance" /> <s:errors
								field="newMember.domainInFrance" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.domainInChina" /></td>
						<td><s:text name="newMember.domainInChina" id="newMember.domainInChina" /> <s:errors
								field="newMember.domainInChina" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.otherDiploma" /></td>
						<td><s:text name="newMember.otherDiploma" id="newMember.otherDiploma" /> <s:errors
								field="newMember.otherDiploma" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.diplomaUniversity" /></td>
						<td><s:text name="newMember.diplomaUniversity" id="newMember.diplomaUniversity" /> <s:errors
								field="newMember.diplomaUniversity" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.firstJobCountry" /></td>
						<td><s:text name="newMember.firstJobCountry" id="newMember.firstJobCountry" /> <s:errors
								field="newMember.firstJobCountry" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.internship" /></td>
						<td><s:text name="newMember.internship" id="newMember.internship" /> <s:errors
								field="newMember.internship" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.firstJob" /></td>
						<td><s:text name="newMember.firstJob" id="newMember.firstJob" /> <s:errors
								field="newMember.firstJob" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.jobCountry" /></td>
						<td><s:text name="newMember.jobCountry" id="newMember.jobCountry" /> <s:errors
								field="newMember.jobCountry" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.employer" /></td>
						<td><s:text name="newMember.employer" id="newMember.employer" /> <s:errors
								field="newMember.employer" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.phone" /></td>
						<td><s:text name="newMember.phone" id="newMember.phone" /> <s:errors
								field="newMember.phone" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.addressSupplement" /></td>
						<td><s:text name="newMember.addressSupplement" id="newMember.addressSupplement" /> <s:errors
								field="newMember.addressSupplement" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.address" /></td>
						<td><s:text name="newMember.address" id="newMember.address" /> <s:errors
								field="newMember.address" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.country" /></td>
						<td><s:text name="newMember.country" id="newMember.country" /> <s:errors
								field="newMember.country" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.weibo" /></td>
						<td><s:text name="newMember.weibo" id="newMember.weibo" /> <s:errors
								field="newMember.weibo" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.weixin" /></td>
						<td><s:text name="newMember.weixin" id="newMember.weixin" /> <s:errors
								field="newMember.weixin" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.notes" /></td>
						<td><s:text name="newMember.notes" id="newMember.notes" /> <s:errors
								field="newMember.notes" /></td>
					</tr>
					
					
					<tr>
						<td><s:label for="newMember.role" /></td>
						<td><s:select name="newMember.roles[0]" id="newMember.role" value="MEMBER">
							<s:options-enumeration enum="afcp.alumni.model.db.Member.Role"/>
								<%-- <s:options-collection collection="${actionBean.roles}"
									value="id" label="label" /> --%>
							</s:select> <s:errors field="newMember.roles[0]" /></td>
					</tr>
					<tr>
						<td><s:label for="newMember.deletable" /></td>
						<td><s:checkbox name="newMember.deletable" id="newMember.deletable" checked="true" /> <s:errors
								field="newMember.deletable" /></td>
					</tr>
					
					
					<tr>
						<td />
						<td>
							<c:choose>
							<c:when test="${not empty actionBean.newMember and not empty actionBean.newMember.id }">
								<s:hidden name="newMemberOldId" />
								<s:submit name="createMember">Modifier</s:submit>
							</c:when>
							<c:otherwise>
								<s:submit name="createMember">Créer</s:submit>
							</c:otherwise>
							</c:choose>
						
						</td>
					</tr>
				</table>
				<%-- <c:if test="${ not empty actionBean.newMember }"><p>Membre créé : ${actionBean.newMember.firstName } ${actionBean.newMember.lastName }</p></c:if> --%>
			</s:form>
		</fieldset>

		<fieldset>
			<legend>Upload d'un fichier de membres</legend>
			<%-- <s:form
				action="<%=BlobstoreServiceFactory.getBlobstoreService()
							.createUploadUrl("/upload")%>"
				method="post" enctype="multipart/form-data"> --%>
			<div>
				<p>Fichier Excel :</p>
				<form
					action="${actionBean.uploadUrl }"
					method="post" enctype="multipart/form-data">
					<s:errors />
					<p>
						<!-- <select name="type">
							<option value="excel">Excel</option>
							<option value="csv">CSV</option>
						</select> -->
						<input type="file" name="membersExcel" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
						<input type="submit" name="uploadexcel" value="Upload" />
					</p>
					<fieldset>
						<legend>Paramètres avancés</legend>
						<div class="collapsed">
							<p>
								Feuille avec les membres : <input type="number" name="excelFile.sheetNumber" value="1" min="1" />
							</p>
							<p>Colonnes du fichier :</p>
							<table>
								
								<%-- <tr>
								<c:forEach items="${actionBean.columns }" var="column">
								</c:forEach>
								</tr> --%>
								<c:forEach items="${actionBean.columns }" var="column">
								<tr>
									<td>
										<label for="excel-<c:out value="${column.id }"/>"><c:out value="${column.id }"/></label>
										<input type="hidden"
											name="excelFile.headers[<c:out value="${column.position - 1 }"/>].id" 
											value="<c:out value="${column.id }"/>" />
									</td>
									<td>
										<input type="text" id="excel-<c:out value="${column.id }"/>" 
											name="excelFile.headers[<c:out value="${column.position - 1 }"/>].position" 
											value="<c:out value="${column.position }"/>" />
									</td>
									<td>
										<input type="text"
											name="excelFile.headers[<c:out value="${column.position - 1 }"/>].label" 
											value="<c:out value="${column.label }"/>" />
										
									</td>
								</tr>
								</c:forEach>
								
							</table>
						</div>
					</fieldset>
				</form>
				<%-- <p>Fichier CSV :</p>
				<form
					action="${actionBean.uploadUrl }"
					method="post" enctype="multipart/form-data">
					<s:errors />
					<p>
						<input type="file" name="membersCSV" accept=".csv" />
						<input type="submit" name="uploadcsv" value="Upload" />
					</p>
					<fieldset>
						<legend>Paramètres avancés</legend>
						<div class="collapsed">
							<p>
								Séparateur
								<select name="csvFile.separator">
									<c:forEach items="${actionBean.separators }" var="separator">
										<option value='<c:out value="${separator }"></c:out>'><c:out value="${separator }"></c:out></option>
									</c:forEach>
								</select>
								Délimiteur
								<select name="csvFile.quote">
									<c:forEach items="${actionBean.quotechars }" var="quotechar">
										<option value='<c:out value="${quotechar }"></c:out>'><c:out value="${quotechar }"></c:out></option>
									</c:forEach>
								</select>
								Échappement
								<select name="csvFile.escape">
									<c:forEach items="${actionBean.escapes }" var="escape">
										<option value='<c:out value="${escape }"></c:out>'><c:out value="${escape }"></c:out></option>
									</c:forEach>
								</select>
								Encodage
								<select name="csvFile.charset">
									<c:forEach items="${actionBean.charsets }" var="charset">
										<option value="${charset }">${charset }</option>
									</c:forEach>
								</select>
							</p>
							<p>Colonnes du fichier :</p>
							<table>
								
								<tr>
								<c:forEach items="${actionBean.columns }" var="column">
									<td><label for="csv-<c:out value="${column.id }"/>"><c:out value="${column.label }"/></label></td>
								</c:forEach>
								</tr>
								<tr>
								<c:forEach items="${actionBean.columns }" var="column">
									<c:if test="${column.required }"><c:set var="required">required="required"</c:set></c:if>
									<td><input type="text" id="csv-<c:out value="${column.id }"/>" 
											name="csvFile.headers['<c:out value="${column.id }"/>']" 
											value="<c:out value="${column.fileColumnName }" />" /></td>
								</c:forEach>
								</tr>
								
							</table>
						</div>
					</fieldset>
				</form> --%>
				
				
				<s:form action="${actionBean.uploadMemberUrl }">
					<s:submit name="resetColumns">Rétablir les colonnes par défaut</s:submit>
				</s:form>
				
			</div>
		</fieldset>

		<fieldset>
			<legend>Liste des membres enregistrés</legend>
			<!-- <div class="collapsed"> -->
			<div>
		
				<p><a href="?export">Exporter les membres au format Excel</a> (lance un téléchargement)</p>
				
				<c:choose>
					<c:when test="${empty actionBean.members.list}">
						<p>Pas de membres.</p>
					</c:when>
					<c:otherwise>
						<table>
							<c:forEach items="${actionBean.members.list}" var="member">
								<c:set var="confirmLabel"><fmt:message bundle="${bundle}" key="confirm.deleteMember"><fmt:param>${member.firstName} ${member.lastName}</fmt:param></fmt:message></c:set>
								<tr>
									<td>
										${member.firstName} ${member.lastName} ${member.chineseName}
										<c:if test="${not empty member.roles }">
											(<c:forEach items="${member.roles}" var="role" 
												varStatus="s"><c:if test="${s.index > 0 }"> </c:if><fmt:message bundle="${bundle}" key="${role}" /></c:forEach>)
										</c:if>
									</td>
									<td>
										<c:if test="${not member.emailSent }">
											<s:form action="${actionBean.manageMemberUrl }">
												<input type="hidden" name="idPasswordMember" value="${member.id }" />
												<s:submit name="generatePassword">Générer le mot de passe</s:submit>
											</s:form>
										</c:if>
									</td>
									<td>
										<s:form beanclass="afcp.alumni.action.page.AdminAction">
											<input type="hidden" name="idModified" value="${member.id }" />
											<s:submit name="modify">Modifier</s:submit>
										</s:form>
									</td> 
									<td>
										<c:if test="${member.deletable}">
											<s:form action="${actionBean.manageMemberUrl }" onsubmit="return confirm('${confirmLabel }');">
												<input type="hidden" name="idDeletedMember" value="${member.id }"></input>
												<s:submit name="removeMember">Supprimer</s:submit> <s:errors
												field="idDeletedMember" />
											</s:form>
										</c:if>
									</td> 
								</tr>
							</c:forEach>
						</table>

						<s:form action="${actionBean.manageMemberUrl }">
								<s:submit name="generateAllPasswords">Générer tous les mots de passe</s:submit> 
								et les envoyer par email. Si le mot de passe 
								ne peut pas être envoyé par email, il ne sera 
								pas généré.
						</s:form>
						</c:otherwise>
				</c:choose>
				
				<%-- pagination --%>
				<p><c:if test="${ actionBean.members.hasPreviousPage }"><a href="${ actionBean.members.previousPageUrl }">Page précédente</a></c:if>
				Page ${ actionBean.members.page } / ${ actionBean.members.pMax }
				<c:if test="${ actionBean.members.hasNextPage }"><a href="${ actionBean.members.nextPageUrl }">Page suivante</a></c:if></p>
				<%-- <p><c:if test="${ actionBean.hasPreviousMemberPage }"><a href="${ actionBean.previousMemberPageUrl }">Page précédente</a></c:if>
				Page ${ actionBean.pm } / ${ actionBean.pmMax }
				<c:if test="${ actionBean.hasNextMemberPage }"><a href="${ actionBean.nextMemberPageUrl }">Page suivante</a></c:if></p> --%>
	
				<%-- <c:if test="${ not empty actionBean.deletedMember }">
					<p>Membre supprimé : ${actionBean.deletedMember.firstName }
						${actionBean.deletedMember.lastName }</p>
				</c:if> --%>
			</div>

		</fieldset>
		
		<h2>Maintenance de la base de données (attention aux bêtises)</h2>

		<c:set var="confirmLabel"><fmt:message bundle="${bundle}" key="confirm.deleteAllMembers" /></c:set>
		<s:form action="${actionBean.manageMemberUrl }" onsubmit="return confirm('${confirmLabel }');">
			<s:submit name="removeAllMembers">Supprimer tous les membres</s:submit> 
			Vous serez déconnecté après cette opération, le compte administrateur 
			étant également supprimé. Il sera recréé à la première tentative de reconnexion.
		</s:form>
		<br/>

		<s:form action="${actionBean.url }">
			<s:errors />
			<s:submit name="reindex">Réindexer les membres</s:submit> Vide l'index de recherche et le remplit à nouveau avec les membres enregistrés en base de données.
		</s:form>

		<%-- <fieldset>
			<legend>Liste des rôles</legend>
			<c:choose>
				<c:when test="${ empty actionBean.roles.list }">
					<p>Pas de rôles.</p>
				</c:when>
				<c:otherwise>
					<ul>
						<c:forEach items="${actionBean.roles.list}" var="role">
							<li>${role.id} : ${role.label} <c:if
									test="${role.deletable}">
									<s:form action="${actionBean.manageMemberUrl }">
										<s:errors />
										<input type="hidden" name="idDeletedRole" value="${role.id }"></input>
										<s:submit name="removeRole">supprimer</s:submit>
									</s:form>
								</c:if>
							</li>
						</c:forEach>
					</ul>
				</c:otherwise>
			</c:choose>
			
			<p><c:if test="${ actionBean.roles.hasPreviousPage }"><a href="${ actionBean.roles.previousPageUrl }">Page précédente</a></c:if>
			Page ${ actionBean.roles.page } / ${ actionBean.roles.pMax }
			<c:if test="${ actionBean.roles.hasNextPage }"><a href="${ actionBean.roles.nextPageUrl }">Page suivante</a></c:if></p>


			<s:form action="${actionBean.manageMemberUrl }">
				<s:errors />
				<table>
					<tr>
						<td><label for="roleId">Identifiant : </label></td>
						<td><s:text name="newRole.id" id="roleId" /> <s:errors
								field="newRole.id" /></td>
					</tr>
					<tr>
						<td><label for="roleLabel">Libellé : </label></td>
						<td><s:text name="newRole.label" id="roleLabel" /> <s:errors
								field="newRole.label" /></td>
					</tr>
					<tr>
						<td><label for="roleDeletable">Supprimable : </label></td>
						<td><s:checkbox name="newRole.deletable" id="roleDeletable" checked="true" /> <s:errors
								field="newRole.deletable" /></td>
					</tr>
					<tr>
						<td />
						<td><s:submit name="addRole">Créer</s:submit>
				</table>
			</s:form>
			
		</fieldset>

		<s:form action="${actionBean.url }">
			<s:errors />
			<s:submit name="resetRoles">Supprimer tous les rôles</s:submit> et rétablir les rôles par défaut. /!\ Attention à supprimer les membres autres qu'administrateurs d'abord !
		</s:form> --%>
		
		<fieldset>
			<legend>Entités stockées en base de données</legend>
			<div>
			
				<c:choose>
					<c:when test="${empty actionBean.kinds}">
						<p>Pas d'entités en base<c:if test="${actionBean.development }"> (<span class="red">Les statistiques Google App Engine ne fonctionnent pas sur un serveur de développement local</span>)</c:if>.</p>
					</c:when>
					<c:otherwise>
						<table>
							<tr>
								<th>Nom</th>
								<th>Nombre</th>
								<th>Taille (octets)</th>
								<th>Supprimer</th>
							</tr>
							<c:forEach items="${actionBean.kinds}" var="kind">
								<tr>
									<td>${kind.kindName}</td>
									<td>${kind.count}</td>
									<td>${kind.bytes}</td>
									<td><a href="?remove&kindName=${kind.kindName}">supprimer</a></td>
								</tr>
							</c:forEach>
						</table>
					</c:otherwise>
				</c:choose>
			
				<p><a href="?clear">Vider les sessions et le cache</a></p>
			
				<p><a href="?clearall">Vider TOUTE la base de données</a></p>
			
			</div>
		</fieldset>
		
		<fieldset>
			<legend>Entités stockées en base de données trouvées par les statistiques</legend>
			<div>
			
				<c:choose>
					<c:when test="${empty actionBean.kindsWithStats}">
						<p>Pas d'entités en base<c:if test="${actionBean.development }"> (<span class="red">Les statistiques Google App Engine ne fonctionnent pas sur un serveur de développement local</span>)</c:if>.</p>
					</c:when>
					<c:otherwise>
						<table>
							<tr>
								<th>Nom</th>
								<th>Nombre</th>
								<th>Taille (octets)</th>
								<th>Supprimer</th>
							</tr>
							<c:forEach items="${actionBean.kindsWithStats}" var="kind">
								<tr>
									<td>${kind.kindName}</td>
									<td>${kind.count}</td>
									<td>${kind.bytes}</td>
									<td><a href="?remove&kindName=${kind.kindName}">supprimer</a></td>
								</tr>
							</c:forEach>
						</table>
					</c:otherwise>
				</c:choose>
			
				<p><a href="?clear">Vider les sessions et le cache</a></p>
			
			</div>
		</fieldset>


	</div>
	
	
	
	
	
	<script type="text/javascript">
	$('fieldset legend').click(function(e) {
		$(this).siblings().slideToggle();
	});
	$('.collapsed').slideToggle(0);
	</script>
	</s:layout-component>

</s:layout-render>